﻿/*
* Актор - звено с передаточной функцией k/(T*s+1)
 * Апериодическое звено первого порядка
* */

using System;
using System.Collections.Generic;
using System.Text;

using ctem.kernel;
using ctem.kernel.lib;
using ctem.kernel.lib.ports.sequential;


namespace ctem.samples.actors.sequential
{
    public class InertiaLink : Actor
    {
        OutPort outport;
        InPort inport;

        DoubleParam step;
        DoubleParam k;
        DoubleParam T;

        //double[] denom = new double[2]; 


        public InertiaLink(string name)
            : base(name)
        {
            outport = new OutPort("outport",this);
            base.RegisterOutport(outport);
            inport = new InPort("inport", this);
            base.RegisterInport(inport);
            step = new DoubleParam("step");
            base.RegisterParam(step);
            step.value = 0.005;
            k = new DoubleParam("k");
            base.RegisterParam(k);
            k.value = 1.0;
            T = new DoubleParam("T");
            base.RegisterParam(T);
            T.value = 1.0;

            //denom[0] = 1.0;
        }

       
        double out_old = 0;

        public override void Fire(double step)
        {
            List<IEntry> d = inport.Get();
            if (d.Count > 0)
            {
                //Console.WriteLine(d[0].data.value);
                out_old = ((double)k.value * (d[0].data.value) + (double)T.value / step * out_old) / ((double)T.value / step + 1);
                //out_old = out_old + ((double)step.value) * inport.Get()[0].value;
                outport.Put(out_old);
            }
//            denom[1] = (double)T.value;
        }
        public override void ReInitialize()
        {
            base.ReInitialize();
            out_old = 0;
        }

        public override void GetWDenom(ref double[] denom)
        {
            denom = new double[2];
            denom[0] = 1.0;
            denom[1] = (double)T.value;
        }


    }
}
